<?php

namespace App\Models\Notice;

use App\Models\BaseModel;

class SmsRecord extends BaseModel
{

    protected $table = 'sms_records';
    protected $fillable = ['template_id', 'user_id', 'task_id', 'type', 'phone', 'content', 'request_id', 'biz_id', 'status', 'data_scope'];
    public $params = ['template_id', 'user_id', 'phone', 'status'];
    public $sorters = ['content', 'created_at'];

    public function template()
    {
        return $this->belongsTo('App\Models\Notice\NoticeTemplate', 'template_id', 'id');
    }

    public function task()
    {
        return $this->belongsTo('App\Models\Task\Task', 'task_id', 'id');
    }

    public function addSmsRecord($insertDataArr)
    {
        return $this->insert($insertDataArr);
    }

    //todo: 排序功能
    public function getMyNoticeList($userId, $limit, $sorter, $search, $type)
    {
        $perPage = $limit > 0 ? $limit : $this->limit;
        $smsRecord = $this->where(['user_id' => $userId])
                ->whereIn('type', $type)
                ->select(['id', 'template_id', 'content', 'created_at'])
                ->orderBy($sorter[0], $sorter[1])
                ->paginate($perPage);
        if ($smsRecord) {
            return $smsRecord->toArray();
        } else {
            return null;
        }
    }

}
